package com.wawayaya.ui;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.creative.accountsdk.UserData;
import com.creative.accountsdk.UserInfor;
import com.creative.accountsdk.holder.Constants;
import com.wawayaya.AppContext;
import com.wawayaya.api.remote.ApiConstans;
import com.wawayaya.base.BaseActivity;
import com.wawayaya.bean.User;
import com.wawayaya.util.TDevice;
import com.wawayaya.util.TLog;
import com.wawayaya.util.UIHelper;
import com.wawayaya.util.event.ShelfEvent;
import de.greenrobot.event.EventBus;
import wawayaya.kids_iread.R;

/**
 * SettingActivity  设置首页
 *
 * @author
 * @version 1.0
 * @created 2015-11-3
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {
    //拼接host
    private final  String   WEB_HOST_PUBLISH = "http://api-joyreader.wawayaya.com";
    /** 设置页面网页地址 (条例、隐私、关于) */
    private final  String  PRIVACY_CN_URL = WEB_HOST_PUBLISH+"/html/Setting_Privacy_Policy_cn.html";
    private final  String  USE_CN_URL  = WEB_HOST_PUBLISH+"/html/Setting_Terms_of_Use_cn.html";
    private final  String  ABOUT_CN_URL  = WEB_HOST_PUBLISH+"/html/Setting_About_JoyReader_cn.html";
    private final  String  PRIVACY_URL = WEB_HOST_PUBLISH+"/html/Setting_Privacy_Policy.html";
    private final  String  USE_URL  = WEB_HOST_PUBLISH+"/html/Setting_Terms_of_Use.html";
    private final  String  ABOUT_URL  = WEB_HOST_PUBLISH+"/html/Setting_About_JoyReader.html";

    private  RelativeLayout rlAddClass;
    private  TextView       tvAddClass;
    private  RelativeLayout rlMyInviteCode;
    private  RelativeLayout rlLogout;
    private  RelativeLayout  rlBack;
    private  RelativeLayout  rlUserInfo;
    private  RelativeLayout  rlBindParent;
    private  TextView        tvBindParent;
    private  RelativeLayout  rlModifyPwd;



    private RelativeLayout  rlSytl;
    private RelativeLayout  rlYszc;
    private RelativeLayout  rlAbout;

    // right web数据相关
    private  RelativeLayout  rlRightAccount;
    private  RelativeLayout  rlRightVip;
    private  RelativeLayout  rlRightRelease;
    private  RelativeLayout  rlRightUse;
    private  RelativeLayout  rlRightPrivate;
    private  RelativeLayout  rlRightAbout;

    //right  Imageview background
    private  ImageView  ivAccount;
    private  ImageView  ivVip;
    private  ImageView  ivRelease;
    private  ImageView  ivUse;
    private  ImageView  ivPrivate;
    private  ImageView  ivAbout;

    //right textview color
    private  TextView  tvAccount;
    private  TextView  tvVip;
    private  TextView  tvRelease;
    private  TextView  tvUse;
    private  TextView  tvPrivate;
    private  TextView  tvAbout;

    private WebView mWebView;
    private ProgressBar pb;

    // content
    private  RelativeLayout  rlWebContent;      //web html
    private  RelativeLayout  rlAccountContent;  //用户相关
    private  RelativeLayout  rlSpaceContent;    //释放空间
    private  RelativeLayout  rlVipContent;      //vip

    private boolean   isZh = false;

    private UserData userData;


    public  static int REQUEST_CODE_BINDPARENT = 101;
    public  static int REQUEST_CODE_ADDCLASS = 102;


   // private
    @Override
    protected void init(Bundle savedInstanceState) {
        setContentView(R.layout.setting_account);
        isZh = TDevice.isZh(context);
        userData = UserData.getInstance();

    }

    @Override
    public void initView(){

        rlAddClass = (RelativeLayout)findViewById(R.id.rl_set_account_addclass);
        tvAddClass = (TextView)findViewById(R.id.tv_add_class);
        if (userData.getRole() != 2) {
            rlAddClass.setVisibility(View.GONE);
        } else {
            if (userData.getClassinfor() == null) {
                tvAddClass.setText(getResources().getString(R.string.set_account_addclass));
            } else {
                tvAddClass.setText(getResources().getString(R.string.set_account_classinfo));
            }
        }

        rlMyInviteCode = (RelativeLayout)findViewById(R.id.rl_set_account_invitecode);
        rlLogout = (RelativeLayout)findViewById(R.id.rl_set_account_exit);
        rlBack = (RelativeLayout)findViewById(R.id.rl_set_account_left);
        rlUserInfo = (RelativeLayout)findViewById(R.id.rl_set_account_info);
        rlBindParent = (RelativeLayout)findViewById(R.id.rl_set_account_bind);
        tvBindParent = (TextView)findViewById(R.id.tv_bind_parent);
        if (userData.getRole() != 2) {
            rlBindParent.setVisibility(View.GONE);
        }  else {
            if (userData.getParentinfor() == null) {
                tvBindParent.setText(getResources().getString(R.string.set_account_bindparent));
            } else {
                tvBindParent.setText(getResources().getString(R.string.set_account_parentinfo));
            }
        }
        rlModifyPwd = (RelativeLayout)findViewById(R.id.rl_set_account_updatepwd);

        rlAccountContent = (RelativeLayout)findViewById(R.id.rl_set_account_content);
        rlSpaceContent = (RelativeLayout)findViewById(R.id.rl_set_space_content);
        rlWebContent = (RelativeLayout)findViewById(R.id.rl_set_web_content);
        rlVipContent = (RelativeLayout)findViewById(R.id.rl_set_vip_content);

        rlSytl= (RelativeLayout)findViewById(R.id.rl_set_sytl);
        rlYszc = (RelativeLayout)findViewById(R.id.rl_set_yszc);
        rlAbout = (RelativeLayout)findViewById(R.id.rl_set_about);

        //web数据相关
        mWebView = (WebView)findViewById(R.id.wv_web_content);
        pb = (ProgressBar) findViewById(R.id.pb_load_webview);

        //right view
        rlRightAccount = (RelativeLayout)findViewById(R.id.rl_set_account_infor);
        rlRightVip = (RelativeLayout)findViewById(R.id.rl_set_vip);
        rlRightRelease = (RelativeLayout)findViewById(R.id.rl_set_release_size);
        rlRightUse = (RelativeLayout)findViewById(R.id.rl_set_sytl);
        rlRightPrivate = (RelativeLayout)findViewById(R.id.rl_set_yszc);
        rlRightAbout = (RelativeLayout)findViewById(R.id.rl_set_about);

        //right Image background
        ivAccount = (ImageView)findViewById(R.id.iv_account_infor);
        ivVip = (ImageView)findViewById(R.id.iv_account_vip);
        ivRelease = (ImageView)findViewById(R.id.iv_account_rels);
        ivUse = (ImageView)findViewById(R.id.iv_account_tl);
        ivPrivate = (ImageView)findViewById(R.id.iv_account_private);
        ivAbout = (ImageView)findViewById(R.id.iv_acount_about);

        //right Textview color
        tvAccount = (TextView)findViewById(R.id.tv_set_account);
        tvVip = (TextView)findViewById(R.id.tv_set_vip);
        tvRelease = (TextView)findViewById(R.id.tv_set_release);
        tvUse = (TextView)findViewById(R.id.tv_set_tl);
        tvPrivate = (TextView)findViewById(R.id.tv_set_private);
        tvAbout = (TextView)findViewById(R.id.tv_set_about);
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        rlAddClass.setOnClickListener(this);
        rlMyInviteCode.setOnClickListener(this);
        rlLogout.setOnClickListener(this);
        rlBack.setOnClickListener(this);
        rlUserInfo.setOnClickListener(this);
        rlBindParent.setOnClickListener(this);
        rlModifyPwd.setOnClickListener(this);

        rlSytl.setOnClickListener(this);
        rlYszc.setOnClickListener(this);
        rlAbout.setOnClickListener(this);

        //right view
        rlRightAccount.setOnClickListener(this);
        rlRightVip.setOnClickListener(this);
        rlRightRelease.setOnClickListener(this);
        rlRightUse.setOnClickListener(this);
        rlRightPrivate.setOnClickListener(this);
        rlRightAbout.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
           switch (v.getId()){
               case R.id.rl_set_account_left:
                   finish();
                   break;
               case R.id.rl_set_account_addclass:
                   if (userData.getClassinfor() == null) {
                       UIHelper.showAccountActivity(SettingActivity.this, Constants.CHILD_ADD_CLASS, REQUEST_CODE_ADDCLASS);
                   } else {
                       UIHelper.showAccountActivity(SettingActivity.this, Constants.CLASS_INFO, REQUEST_CODE_ADDCLASS);
                   }
                   break;
               case R.id.rl_set_account_invitecode:
                   UIHelper.showAccountActivity(SettingActivity.this, Constants.MY_INVITE_CODE, 0);
                   break;
               case  R.id.rl_set_account_exit:
                   AppContext.getInstance().Logout();
                   SettingActivity.this.finish();
                   refreshTitleBar();
                   break;
               case R.id.rl_set_account_infor:
                   cleanBgColor();
                   ivAccount.setImageResource(R.drawable.setting_btn_account_down);
                   tvAccount.setTextColor(ContextCompat.getColor(context, R.color.setting_lan_bg));
                   rlRightAccount.setBackgroundColor(ContextCompat.getColor(context, R.color.setting_font_click_bg));
                   showAccount();
                   break;
               case R.id.rl_set_vip:
                   cleanBgColor();
                   ivVip.setImageResource(R.drawable.setting_btn_vip_down);
                   tvVip.setTextColor(ContextCompat.getColor(context, R.color.setting_lan_bg));
                   rlRightVip.setBackgroundColor(ContextCompat.getColor(context, R.color.setting_font_click_bg));
                   showVipContent();
                   break;
               case R.id.rl_set_release_size:
                   cleanBgColor();
                   ivRelease.setImageResource(R.drawable.setting_btn_space_down);
                   tvRelease.setTextColor(ContextCompat.getColor(context, R.color.setting_lan_bg));
                   rlRightRelease.setBackgroundColor(ContextCompat.getColor(context, R.color.setting_font_click_bg));
                   releaseSpace();
                   break;
               case R.id.rl_set_sytl:
                   cleanBgColor();
                   ivUse.setImageResource(R.drawable.setting_btn_rules_down);
                   tvUse.setTextColor(ContextCompat.getColor(context, R.color.setting_lan_bg));
                   rlRightUse.setBackgroundColor(ContextCompat.getColor(context, R.color.setting_font_click_bg));
                   showWeb(v.getId());
                   break;
               case R.id.rl_set_yszc:
                   cleanBgColor();
                   ivPrivate.setImageResource(R.drawable.setting_btn_secret_down);
                   tvPrivate.setTextColor(ContextCompat.getColor(context, R.color.setting_lan_bg));
                   rlRightPrivate.setBackgroundColor(ContextCompat.getColor(context, R.color.setting_font_click_bg));
                   showWeb(v.getId());
                   break;
               case R.id.rl_set_about:
                   cleanBgColor();
                   ivAbout.setImageResource(R.drawable.setting_btn_about_down);
                   tvAbout.setTextColor(ContextCompat.getColor(context, R.color.setting_lan_bg));
                   rlRightAbout.setBackgroundColor(ContextCompat.getColor(context, R.color.setting_font_click_bg));
                   showWeb(v.getId());
                   break;
               case R.id.rl_set_account_info:
                   UIHelper.showAccountActivity(SettingActivity.this, Constants.USERINFO, 0);
                   break;
               case R.id.rl_set_account_bind:
                   if (userData.getParentinfor() == null){
                       UIHelper.showAccountActivity(SettingActivity.this, Constants.CHILD_BIND_PARENT_ACCOUNT, REQUEST_CODE_BINDPARENT);
                   } else {
                       UIHelper.showAccountActivity(SettingActivity.this, Constants.UNBIND_ACCOUNT, REQUEST_CODE_BINDPARENT);
                   }
                   break;
               case R.id.rl_set_account_updatepwd:
                   UIHelper.showAccountActivity(SettingActivity.this, Constants.MODIFY_PASSWORD, 0);
                   break;
           }
    }

    private void  showAccount(){
        rlAccountContent.setVisibility(View.VISIBLE);
        rlWebContent.setVisibility(View.GONE);
        rlSpaceContent.setVisibility(View.GONE);
        rlVipContent.setVisibility(View.GONE);
    }

    private void  showVipContent(){
        rlAccountContent.setVisibility(View.GONE);
        rlWebContent.setVisibility(View.GONE);
        rlSpaceContent.setVisibility(View.GONE);
        rlVipContent.setVisibility(View.VISIBLE);
    }

    private void  refreshTitleBar( ){
        ShelfEvent event = new ShelfEvent(ShelfEvent.refreshParentIcon);
        EventBus.getDefault().post(event);
    }


    /**
     * 切换 web view
     */
    private void  showWeb(int target){
        rlSpaceContent.setVisibility(View.GONE);
        rlAccountContent.setVisibility(View.GONE);
        rlWebContent.setVisibility(View.VISIBLE);
        rlVipContent.setVisibility(View.GONE);
        String  url ="";
        if(target == R.id.rl_set_sytl){
            if(isZh){
                url = USE_CN_URL;
            }else {
                url = USE_URL;
            }
        }else if(target == R.id.rl_set_yszc){
            if(isZh){
                url = PRIVACY_CN_URL;
            }else {
                url = PRIVACY_URL;
            }
        }else if(target == R.id.rl_set_about){
            if(isZh){
                url = ABOUT_CN_URL;
            }else {
                url = ABOUT_URL;
            }
        }
        showView(url);
    }

    /**
     * 按url 显示  webview
     * @param url
     */
    public void  showView(String  url){
        mWebView.setBackgroundColor(0);
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                pb.setVisibility(View.VISIBLE);
                mWebView.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("@")) {
                    Intent data = new Intent(Intent.ACTION_SENDTO);
                    data.setData(Uri.parse("mailto:" + url));
                    data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(data);
                } else {
                    // view.loadUrl(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
                //dismiss();
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pb.setVisibility(View.GONE);
                mWebView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
    }


    /**
     * 释放空间
     */
    private void  releaseSpace(){
        rlAccountContent.setVisibility(View.GONE);
        rlWebContent.setVisibility(View.GONE);
        rlSpaceContent.setVisibility(View.VISIBLE);
        rlVipContent.setVisibility(View.GONE);
    }

    /**
     * 切换改变背影
     */
    private void  cleanBgColor(){

        rlRightAccount.setBackgroundColor (ContextCompat.getColor(context, R.color.white));
        rlRightVip.setBackgroundColor (ContextCompat.getColor(context, R.color.white));
        rlRightRelease.setBackgroundColor (ContextCompat.getColor(context, R.color.white));
        rlRightUse.setBackgroundColor (ContextCompat.getColor(context, R.color.white));
        rlRightPrivate.setBackgroundColor (ContextCompat.getColor(context, R.color.white));
        rlRightAbout.setBackgroundColor (ContextCompat.getColor(context, R.color.white));

        ivAccount.setImageResource(R.drawable.setting_btn_account_normal);
        ivVip.setImageResource(R.drawable.setting_btn_vip_normal);
        ivRelease.setImageResource(R.drawable.setting_btn_space);
        ivUse.setImageResource(R.drawable.setting_btn_rules_normal);
        ivPrivate.setImageResource(R.drawable.setting_btn_secret_normal);
        ivAbout.setImageResource(R.drawable.setting_btn_about_normal);

        tvAccount.setTextColor(ContextCompat.getColor(context, R.color.setting_right_font));
        tvVip.setTextColor(ContextCompat.getColor(context, R.color.setting_right_font));
        tvRelease.setTextColor(ContextCompat.getColor(context, R.color.setting_right_font));
        tvUse.setTextColor(ContextCompat.getColor(context, R.color.setting_right_font));
        tvPrivate.setTextColor(ContextCompat.getColor(context,R.color.setting_right_font));
        tvAbout.setTextColor(ContextCompat.getColor(context, R.color.setting_right_font));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADDCLASS) {
            if (userData.getClassinfor() == null) {
                tvAddClass.setText(getResources().getString(R.string.set_account_addclass));
            } else {
                tvAddClass.setText(getResources().getString(R.string.set_account_classinfo));
            }
        } else if (requestCode == REQUEST_CODE_BINDPARENT) {
            if (userData.getParentinfor() == null) {
                tvBindParent.setText(getResources().getString(R.string.set_account_bindparent));
            } else {
                tvBindParent.setText(getResources().getString(R.string.set_account_parentinfo));
            }
        }
    }
}
